#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
    int total_num = 0;
    int order_type = 0;
    while (cin >> total_num >> order_type) {
        vector<vector<string> > tmp(101);
        string name = "";
        int score = 0;
        for (int i = 0; i < total_num; i++) {
            cin >> name >> score;
            tmp[score].push_back(name);
        }
        if (order_type == 1) {
            for (int i = 0; i < 101; i++) {
                for (int j = 0; j < tmp[i].size(); j++) {
                    cout << tmp[i][j] << " " << i << endl;
                }
            }
        } else {
            for (int i = 100; i >= 0; i--) {
                for (int j = 0; j < tmp[i].size(); j++) {
                    cout << tmp[i][j] << " " << i << endl;
                }
            }
        }
    }
    return 0;
}